﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SlimCode.Windows.Forms.Mobile;

namespace SlimCode.SlimPasswords
{
  internal partial class PasswordInfoForm : Form
  {
    private PasswordItem m_item;
    private IScreenHost m_host;
    private SizeF m_fontSize = SizeF.Empty;

    public PasswordInfoForm()
    {
      InitializeComponent();

      Program.UpdateTemplates( this.Controls );

      closeButton.Item.Image = ImageCache.GetCloseImage( closeButton.ClientSize.Height - 10 );

      this.MoveUp();
    }

    public PasswordInfoForm( PasswordItem item, IScreenHost host )
      : this()
    {
      if( item == null )
        throw new ArgumentNullException( "item" );

      if( host == null )
        throw new ArgumentNullException( "host" );

      m_item = item;
      m_host = host;

      if( !string.IsNullOrEmpty( item.UserName ) )
      {
        passwordInfoList.Items.Add( new PasswordInfoView( "Username:", item.UserName ) );
      }

      if( !string.IsNullOrEmpty( item.Email ) )
      {
        passwordInfoList.Items.Add( new PasswordInfoView( "Email:", item.Email ) );
      }

      if( !string.IsNullOrEmpty( item.Password ) )
      {
        passwordInfoList.Items.Add( new PasswordInfoView( "Password:", item.Password ) );
      }

      if( !string.IsNullOrEmpty( item.UrlAddress ) )
      {
        passwordInfoList.Items.Add( new LinkPasswordInfoView( "Web address:", item.UrlAddress ) );
      }

      /*
      if( !string.IsNullOrEmpty( item.Notes ) )
      {
        passwordInfoList.Items.Add( new PasswordInfoView( string.Empty, item.Notes ) );
      }
      */
    }

    private void PasswordInfoForm_KeyDown( object sender, KeyEventArgs e )
    {
      if( ( e.KeyCode == System.Windows.Forms.Keys.Enter ) )
      {
        this.Close();
        return;
      }

      if( ( e.KeyCode == System.Windows.Forms.Keys.Up ) )
      {
        this.MoveUp();
      }
      else if( ( e.KeyCode == System.Windows.Forms.Keys.Down ) )
      {
        this.MoveDown();
      }
    }

    private void UpdateFontSize()
    {
      if( m_fontSize.IsEmpty )
      {
        using( Graphics graphics = this.CreateGraphics() )
        {
          m_fontSize = graphics.MeasureString( "W", this.Font );
        }
      }
    }

    private void MoveUp()
    {
      this.UpdateFontSize();

      this.Location = new Point( Screen.PrimaryScreen.WorkingArea.Left + 30, Screen.PrimaryScreen.WorkingArea.Top );
      this.Size = new Size( Screen.PrimaryScreen.WorkingArea.Width - 30, ( int )m_fontSize.Height * 3 );
    }

    private void MoveDown()
    {
      this.UpdateFontSize();

      int height = ( int )m_fontSize.Height * 3;

      // height * 2: Leaving room for menu bar, approximate way.
      this.Location = new Point( Screen.PrimaryScreen.WorkingArea.Left + 30, Screen.PrimaryScreen.WorkingArea.Bottom - height * 2 );
      this.Size = new Size( Screen.PrimaryScreen.WorkingArea.Width - 30, height );
    }

    /*
    private void userNameLink_Click( object sender, EventArgs e )
    {
      if( ( m_item != null ) && ( !string.IsNullOrEmpty( m_item.UserName ) ) )
      {
        Clipboard.SetDataObject( m_item.UserName );
        userNameLink.ForeColor = Color.LightGreen;
      }
    }

    private void emailLink_Click( object sender, EventArgs e )
    {
      if( ( m_item != null ) && ( !string.IsNullOrEmpty( m_item.Email ) ) )
      {
        Clipboard.SetDataObject( m_item.Email );
        emailLink.ForeColor = Color.LightGreen;
      }
    }

    private void passwordLink_Click( object sender, EventArgs e )
    {
      if( ( m_item != null ) && ( !string.IsNullOrEmpty( m_item.Password ) ) )
      {
        Clipboard.SetDataObject( m_item.Password );
        passwordLink.ForeColor = Color.LightGreen;
      }
    }
    */

    private void closeButton_Click( object sender, EventArgs e )
    {
      Clipboard.SetDataObject( string.Empty );
      this.Close();
    }

    private void passwordInfoList_SelectedItemChanged( object sender, EventArgs e )
    {
      if( passwordInfoList.SelectedItem != null )
      {
        Point offset = passwordInfoList.Offset;

        offset.X += passwordInfoList.SelectedItem.ClientRectangle.Left;
        passwordInfoList.SetOffset( offset, true );
      }
    }

    private void passwordInfoList_ItemClick( object sender, SlimCode.Windows.Controls.Mobile.ItemEventArgs args )
    {
      if( args.Item is LinkPasswordInfoView )
      {
        Clipboard.SetDataObject( m_item.Password );

        try
        {
          System.Diagnostics.Process.Start( args.Item.Text, string.Empty );
        }
        catch { }
      }
      else
      {
        Clipboard.SetDataObject( args.Item.Text );
      }
    }
  }
}
